package com.lanou.musicproject.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.musicproject.R;
import com.lanou.musicproject.bean.Mv;
import com.lanou.musicproject.bean.MvViedeo;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 王世美 on 15/10/23.
 * mv播放页面
 */
public class  ViedeoActivity extends Activity {
    // 初始化组件
    @Bind(R.id.videoView)
    VideoView videoView;
    // 播放视频组件控制器
    private MediaController mMediaController;
    // 网络接口地址
    private String path = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=" +
            "android&version=5.6.5.0&method=baidu.ting.mv.playMV&format=json&mv_id=";
    private String path1 = "&song_id=&definition=0";
    // 网址拼接id
    private String mv_id;
    private Uri uri;
    private String sourcepath;
    private String url;
    private String source;
    // 请求队列
    private RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //让屏幕横向显示
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(R.layout.activity_viedeo_view);
        requestQueue = Volley.newRequestQueue(this);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        Mv.ResultEntity.MvListEntity entity = (Mv.ResultEntity.MvListEntity)
                intent.getSerializableExtra("entity");
        mv_id = entity.getMv_id();
        this.addData();



    }

    // 解析实体类数据
    public void addData() {
        String getUrl = path + mv_id + path1;
        StringRequest request = new StringRequest(getUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response != null) {
                    Gson gson = new Gson();
                    MvViedeo result = gson.fromJson(response, MvViedeo.class);
                    if (result != null) {
                        sourcepath = result.getResult().getVideo_info().getSourcepath();
                        source = sourcepath.substring(31);
                        url = "http://www.yinyuetai.com/mv/video-url/" + source;
                        Log.i("lanou", url);
                        //该组件可以管理视频的播放,暂停等操作,不需要实现
                        mMediaController = new MediaController(ViedeoActivity.this);
                        uri = Uri.parse(url);
                        //videoView.setVideoURI(uri);
                        videoView.setMediaController(mMediaController);
                        //mMediaController.setMediaPlayer(videoView);
                        videoView.setVideoURI(uri);
                        //开始播放
                        videoView.start();
                        //让videoView获得焦点
                        videoView.requestFocus();

                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }

        });
        requestQueue.add(request);

    }
}
